package com.qmscy.sparrow.common.util;


import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * 常用校验工具
 *
 * @Author: wangxiaonan
 * @Date: 2019/1/6
 **/
public class ValidateUtil {

    /**
     * 正则表达式：校验手机号
     */
    public static final String REGEX_MOBILE = "^((13[0-9])|(14[0-9])|(15[^4,\\d])|(17[0-9])|(18[0-9]))\\d{8}$";

    /**
     * 正则表达式：校验邮箱
     */
    public static final String REGEX_EMAIL = "^[0-9a-z]+\\w*@([0-9a-z]+\\.)+[0-9a-z]+$";
    /**
     * 正则表达式：校验用户名
     */
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{3,30}$";


    /**
     * 校验手机号
     */
    public static boolean isMobileNo(String mobileNo) {
        boolean flag = false;
        if (StringUtils.isNotBlank(mobileNo)) {
            flag = Pattern.matches(REGEX_MOBILE, mobileNo);
        }
        return flag;
    }

    /**
     * 校验邮箱
     */
    public static boolean isEmail(String emailNo) {
        boolean flag = false;
        if (StringUtils.isNotBlank(emailNo)) {
            flag = Pattern.matches(REGEX_EMAIL, emailNo);
        }
        return flag;
    }

    /**
     * 校验用户名，5-30个字符
     */
    public static boolean isValidUsername(String username) {
        boolean flag = false;
        if (StringUtils.isNotBlank(username)) {
            flag = Pattern.matches(REGEX_USERNAME, username);
        }
        return flag;
    }

}